package com.example.personalcenter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 新闻列表适配器
 */
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
    
    private List<NewsItem> newsList = new ArrayList<>();
    private OnItemClickListener clickListener;
    private OnItemLongClickListener longClickListener;

    /**
     * 列表项点击监听接口
     */
    public interface OnItemClickListener {
        void onItemClick(NewsItem item);
    }

    /**
     * 列表项长按监听接口
     */
    public interface OnItemLongClickListener {
        void onItemLongClick(NewsItem item);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.clickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.longClickListener = listener;
    }

    /**
     * 更新列表数据
     */
    public void setNewsList(List<NewsItem> newsList) {
        this.newsList = newsList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_news, parent, false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        NewsItem item = newsList.get(position);
        holder.bind(item);
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onItemClick(item);
            }
        });
        
        // 长按事件
        holder.itemView.setOnLongClickListener(v -> {
            if (longClickListener != null) {
                longClickListener.onItemLongClick(item);
                return true;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    /**
     * ViewHolder
     */
    static class NewsViewHolder extends RecyclerView.ViewHolder {
        
        private TextView tvTitle;
        private TextView tvContent;
        private TextView tvAuthor;
        private TextView tvTime;

        public NewsViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_news_title);
            tvContent = itemView.findViewById(R.id.tv_news_content);
            tvAuthor = itemView.findViewById(R.id.tv_news_author);
            tvTime = itemView.findViewById(R.id.tv_news_time);
        }

        public void bind(NewsItem item) {
            tvTitle.setText(item.getTitle());
            tvContent.setText(item.getContent());
            tvAuthor.setText("作者: " + item.getAuthor());
            tvTime.setText(item.getPublishTime());
        }
    }
}
